package org.osforce.platform.action;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author gavin
 * 
 */
public class NavigatorAction extends ActionSupport{

	private static final long serialVersionUID=-3968444599184093876L;
	private static final Logger logger=LoggerFactory
			.getLogger(NavigatorAction.class);

	public NavigatorAction(){
	}
	
	@Actions({
			@Action(value="/*",results={@Result(name=SUCCESS,type="freemarker",location="${requestPath}")}),
			@Action(value="/administractor/*",results={@Result(name=SUCCESS,type="freemarker",location="${requestPath}")}),
			@Action(value="/ucenter/*",results={@Result(name=SUCCESS,type="freemarker",location="${requestPath}")})})
	public String execute() throws Exception{
		System.out.println("index in execute");
		return SUCCESS;
	}
	
	public String getRequestPath(){
		String requestResource=null;
		// String
		// contextPath=ServletActionContext.getRequest().getContextPath();
		String requestURI=ServletActionContext.getRequest().getRequestURI();
		String resourceName=StringUtils.substringAfterLast(requestURI,"/");
		if(StringUtils.isNotBlank(resourceName)
				&&StringUtils.contains(resourceName,"html")){
			resourceName=StringUtils.removeEnd(resourceName,"html");
			requestResource=resourceName.concat("ftl");
			logger.info("请求的资源为 "+requestResource);
		}else{
			requestResource="/404page.ftl";
			logger.warn("在服务器端没有找到请求的资源！");
		}

		/*
		 * if(StringUtils.contains(requestURI,contextPath)
		 * &&StringUtils.contains(requestURI,"html")
		 * &&StringUtils.startsWith(requestURI,contextPath)
		 * &&StringUtils.endsWith(requestURI,"html")){
		 * requestPath=StringUtils.replaceOnce(requestURI,contextPath,"");
		 * requestPath=StringUtils.removeEnd(requestPath,"html");
		 * requestPath=requestPath.concat("ftl");
		 * logger.info("请求的资源为 "+requestPath); }else{
		 * requestPath="/404page.ftl"; logger.warn("在服务器端没有找到请求的资源！"); }
		 */
		return requestResource;
	}
}
